<template>
  <el-dialog
    class="add-project-info-comp"
    title="编辑"
    :close-on-click-modal="false"
    :append-to-body="true"
    :destroy-on-close="true"
    :visible="visible"
    :width="$setRem(450)"
    :before-close="handleCancel">
    <el-form
      :model="ruleForm"
      :rules="rules"
      size="medium"
      ref="ruleForm"
      :label-width="$setRem(120)"
      class="rule-form"
    >
      <el-form-item
        label="项目名称"
        prop="projectName"
        required>
        <el-input
          clearable
          placeholder="请输入项目名称"
          v-model="ruleForm.projectName">
        </el-input>
      </el-form-item>
      <el-form-item
        label="角色名称"
        prop="roleId"
        required>
        <el-select
          filterable
          remote
          v-model="ruleForm.roleId"
          placeholder="请选择角色名称">
          <el-option
            v-for="item in roleList"
            :key="item.id"
            :label="item.roleName"
            :value="item.id">
            {{ item.roleName }}
          </el-option>
        </el-select>
        <el-tooltip
          class="tooltip-item"
          effect="dark"
          content="下拉选项中无角色名称，请先在项目管理-项目角色模块中维护信息"
          placement="top-start">
          <i class="el-icon-question"></i>
        </el-tooltip>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button
        size="medium"
        @click="handleCancel">取 消</el-button>
      <el-button
        size="medium"
        :loading="loading"
        type="primary"
        @click="hanldeSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    // 弹框是否展示
    visible: {
      type: Boolean,
      default: true
    },
    loading: {
      type: Boolean,
      default: false
    },
    // 回填数据
    options: {
      type: Object,
      default: () => ({})
    },
    // 新增、编辑
    type: {
      type: String,
      default: 'edit'
    }
  },
  watch: {
    options: {
      deep: true,
      handler(data) {
        if (data) {
          const keys = Object.keys(this.ruleForm);
          Object.keys(data).length && keys.forEach(key => {
            this.ruleForm[key] = data[key] || '';
          });
        }
      }
    },
    visible(val) {
      if (!val) {
        this.ruleForm = {
          id: '',
          projectId: '',
          projectName: '',
          roleId: ''
        };
        return;
      }
      this.getRoleList();
    },
  },
  data() {
    return {
      roleList: [], // 角色信息
      ruleForm: {
        id: '',
        projectId: '',
        projectName: '',
        roleId: ''
      },
      rules: {
        projectName: [
          { required: true, message: '请输入项目名称' }
        ],
        roleId: [
          { required: true, message: '请选择角色' }
        ],
      }
    };
  },
  methods: {
    /**
     * 获取权限信息
     */
    async getRoleList(params = {}) {
      const { success, data: {
        data
      }} = await this.$Model.getRoleList({
        pageFlag: false,
        roleType: '2', // （1：系统角色；2：项目角色）
        ...params
      });
      if (success && data) {
        this.roleList = data.list || [];
      }
    },
    // 搜索过滤
    remoteMethod(query) {
      if (query !== '') {
        // this.loading = true;
        setTimeout(async () => {
          this.getRoleList();
        }, 200);
        return;
      }
      this.searchList = [];
    },
    handleCancel() {
      this.$emit('cancel', true);
    },
    hanldeSave() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$emit('submit', this.ruleForm);
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
  @import "../../assets/styles/dialog";

  .add-project-info-comp {
    /deep/ .el-form-item {
      .el-form-item__content {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        > .el-input, .el-select {
          width: calc(100% - 40px);
        }
      }
    }
    .el-icon-question {
      width: 30px;
      font-size: 20px;
      cursor: pointer;
      color: $mainColor;
      text-align: center;
      margin-left: 6px;
    }
  }
</style>
